﻿/// /////////////////////////////////////////////
// 单元名 : unitConfig
// 作者 : Hawk
// 单元描述 :
// 日期 : 2023-09-15
/// /////////////////////////////////////////////

{$IFDEF FPC}
{$H+}
{$MODE DELPHI}
{$ENDIF}
unit unitConfig;

interface

uses
{$IFnDEF FPC}
  Windows,
{$ELSE}
  LCLIntf, LCLType, LMessages,
{$ENDIF}
  unitCommonDataStruct, unitIOCPDataObj, unitClientBuffer;

type
  TConfigFile = class(TClientBufferFile)
  private
    FProductID: TID_OBJ;
    procedure SetProductID(const Value: TID_OBJ);
  protected
    procedure DoReadFromPack(PropertyIdx, Len: Longword; Buf: TIOCPDataCollecter); override;
    procedure DoWritePropertyToPack(dp: TIOCPDataCollecter); override;
    function ConfigFileName: string;
  public
    procedure LoadConfig;
    procedure SaveConfig;

    property ProductID: TID_OBJ read FProductID write SetProductID;
  end;

implementation

uses SysUtils, unitObjPool, unitMisc;

const
  STR_CONFIGFILENAME = '%appdata%\CPlatform\CPlatform.config';

  { TConfigFile }

function TConfigFile.ConfigFileName: string;
var
  a: array [0 .. MAX_PATH] of Char;
begin
  a[0] := #0;
  ExpandEnvironmentStrings(STR_CONFIGFILENAME, a, MAX_PATH);
  Result := a;
end;

procedure TConfigFile.DoReadFromPack(PropertyIdx, Len: Longword; Buf: TIOCPDataCollecter);
begin
  case PropertyIdx of
    1:
      Buf.ReadBuf(FProductID, Len);
  else
    inherited;
  end;
end;

procedure TConfigFile.DoWritePropertyToPack(dp: TIOCPDataCollecter);
begin
  inherited;
  WritePropertyToPack(dp, 1, SizeOf(FProductID), FProductID);
end;

procedure TConfigFile.LoadConfig;
begin
  ReadFromFile(ConfigFileName);
end;

procedure TConfigFile.SaveConfig;
var
  s: string;
begin
  s := ConfigFileName;
  CreatePath(s);
  WriteToFile(s);
end;

procedure TConfigFile.SetProductID(const Value: TID_OBJ);
begin
  if FProductID <> Value then
  begin
    FProductID := Value;
    DoModifyed;
  end;
end;

end.
